package org.ielse.frame.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.ielse.frame.BaseActivity;
import org.ielse.frame.R;
import org.ielse.frame.t.view.recycler.DividerItemDecoration;
import org.ielse.frame.t.view.recycler.paulburke.helper.ItemTouchHelperAdapter;
import org.ielse.frame.t.view.recycler.paulburke.helper.ItemTouchHelperViewHolder;
//import org.ielse.frame.t.view.recycler.paulburke.helper.SimpleItemTouchHelperCallback;
import org.ielse.frame.t.view.yugy.RevealLayout;
import org.ielse.frame.test.RecyclerActivity.RecyclerAdapter2.OnStartDragListener;
import org.ielse.frame.util.LogUtil;

import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.StaggeredGridLayoutManager;
//import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RecyclerActivity extends BaseActivity {

	private RevealLayout revealLayout;

	private RecyclerView recyclerView1;
	private RecyclerView recyclerView2;
	private RecyclerAdapter2 adapter2;
	private RecyclerAdapter1 adapter1;

	private LinearLayoutManager layoutManager;
	private StaggeredGridLayoutManager staggreLayoutManager;
	
//	 private ItemTouchHelper mItemTouchHelper;

	private final List<MData> valueList = new ArrayList<MData>();
	ArrayList<MData> data2List = new ArrayList<MData>();

	@Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_recycler);
		super.onCreate(savedInstanceState);

		revealLayout = (RevealLayout) findViewById(R.id.lay_reveal);

		for (int i = 0; i < 22; i++) {
			MData md = new MData();
			md.content = "item " + i;
			valueList.add(md);
			MData md2 = new MData();
			md.content = "item " + i;
			data2List.add(md2);
		}
		//
		layoutManager = new LinearLayoutManager(this);
		layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
		recyclerView1 = (RecyclerView) findViewById(R.id.view_recycler1);
		recyclerView1.setHasFixedSize(true);
		recyclerView1.setLayoutManager(layoutManager);

		recyclerView1.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST));
		recyclerView1.setItemAnimator(new DefaultItemAnimator());
		recyclerView1.setAdapter(adapter1 = new RecyclerAdapter1(data2List));

		findViewById(R.id.btn_next).setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
			}
		});

		findViewById(R.id.btn_last).setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
				revealLayout.next((int) v.getX() + v.getWidth() / 2, (int) v.getY() + v.getHeight() / 2, 1000);
			}
		});

		// ///////////////////////////////////////////

		recyclerView2 = (RecyclerView) findViewById(R.id.view_recycler2);
		recyclerView2.setHasFixedSize(true);
		staggreLayoutManager = new StaggeredGridLayoutManager(6, StaggeredGridLayoutManager.VERTICAL);
		staggreLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
		recyclerView2.setItemAnimator(new DefaultItemAnimator());
		recyclerView2.setLayoutManager(staggreLayoutManager);
		recyclerView2.setAdapter(adapter2 = new RecyclerAdapter2(valueList));
//		adapter2.setOnDragStartListener(new OnStartDragListener() {
//			@Override public void onStartDrag(ViewHolder viewHolder) {
//				mItemTouchHelper.startDrag(viewHolder);
//			}
//		});
//		ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter2);
//        mItemTouchHelper = new ItemTouchHelper(callback);
//        mItemTouchHelper.attachToRecyclerView(recyclerView2);
		
		findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
				revealLayout.next((int) v.getX() + v.getWidth() / 2, (int) v.getY() + v.getHeight() / 2, 1000);
			}
		});

		findViewById(R.id.btn_fun).setOnClickListener(new OnClickListener() {
			@Override public void onClick(View v) {
				List<MData> delList = adapter2.querySelectedValue();
				for (MData mData : delList) {
					adapter2.remove(mData);
				}
			}
		});
	}

	public class MData {
		String content;
		boolean select;
	}

	// ItemTouchHelperViewHolder
	public static class RecyclerAdapter1 extends RecyclerView.Adapter<RecyclerAdapter1.ViewHolder1> {
		private LinearLayout.LayoutParams lpWrapper = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		private List<MData> valueList;

		public RecyclerAdapter1(List<MData> valueList) {
			this.valueList = valueList;
		}

		public void add(MData item, int position) {
			valueList.add(position, item);
			notifyItemInserted(position);
		}

		public void remove(MData item) {
			int position = valueList.indexOf(item);
			valueList.remove(position);
			notifyItemRemoved(position);
			notifyItemRangeChanged(position, getItemCount());
		}

		public List<MData> querySelectedValue() {
			List<MData> selectedList = new ArrayList<RecyclerActivity.MData>();
			for (MData mData : valueList) {
				if (mData.select) selectedList.add(mData);
			}
			return selectedList;
		}

		public interface OnRecyclerViewListener {
			void onItemClick(int position);
		}

		private OnRecyclerViewListener onRecyclerViewListener;

		public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
			this.onRecyclerViewListener = onRecyclerViewListener;
		}

		public class ViewHolder1 extends RecyclerView.ViewHolder implements OnClickListener {
			public ViewHolder1(View view) {
				super(view);
				view.setOnClickListener(this);
			}

			int position;
			android.widget.ImageView mImg;
			TextView mTxt;

			@Override public void onClick(View v) {
				final MData md = valueList.get(position);
				md.select = !md.select;

				v.setBackgroundColor(md.select ? 0X33000000 : 0Xffffffff);

				if (null != onRecyclerViewListener) onRecyclerViewListener.onItemClick(position);
			}
		}

		@Override public int getItemCount() {
			return valueList.size();
		}

		@Override public ViewHolder1 onCreateViewHolder(ViewGroup viewGroup, int i) {
			View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_recycler, viewGroup, false);
			ViewHolder1 viewHolder = new ViewHolder1(view);
			view.setLayoutParams(lpWrapper);
			viewHolder.mImg = (android.widget.ImageView) view.findViewById(R.id.img_anim1);
			viewHolder.mTxt = (TextView) view.findViewById(R.id.tx_test_3);
			return viewHolder;
		}

		@Override public void onBindViewHolder(final ViewHolder1 viewHolder, final int i) {
			final MData md = valueList.get(i);
			viewHolder.position = i;
			viewHolder.mTxt.setText(md.content);
			viewHolder.itemView.setBackgroundColor(md.select ? 0X33000000 : 0Xffffffff);
		}
	}

	public static class RecyclerAdapter2 extends RecyclerView.Adapter<RecyclerAdapter2.ViewHolder2> implements ItemTouchHelperAdapter {
		private final LinearLayout.LayoutParams lpWrapper = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		private OnRecyclerViewListener onRecyclerViewListener;
		private OnStartDragListener onDragStartListener;
		private List<MData> valueList;

		public RecyclerAdapter2(List<MData> valueList) {
			this.valueList = valueList;
		}

		public void add(MData item, int position) {
			valueList.add(position, item);
			notifyItemInserted(position);
		}

		public void remove(MData item) {
			int position = valueList.indexOf(item);
			valueList.remove(position);
			notifyItemRemoved(position);
			notifyItemRangeChanged(position, getItemCount());
		}

		public List<MData> querySelectedValue() {
			List<MData> selectedList = new ArrayList<RecyclerActivity.MData>();
			for (MData mData : valueList) {
				if (mData.select) selectedList.add(mData);
			}
			return selectedList;
		}

		public interface OnRecyclerViewListener {
			void onItemClick(int position);
		}

		public interface OnStartDragListener {
			void onStartDrag(RecyclerView.ViewHolder viewHolder);
		}

		public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
			this.onRecyclerViewListener = onRecyclerViewListener;
		}

		public void setOnDragStartListener(OnStartDragListener onDragStartListener) {
			this.onDragStartListener = onDragStartListener;
		}

		public class ViewHolder2 extends RecyclerView.ViewHolder implements OnClickListener, ItemTouchHelperViewHolder {
			public ViewHolder2(View view) {
				super(view);
				view.setOnClickListener(this);
			}

			int position;
			android.widget.ImageView mImg;
			TextView mTxt;

			@Override public void onClick(View v) {
				final MData md = valueList.get(position);
				md.select = !md.select;

				v.setBackgroundColor(md.select ? 0X33000000 : 0Xffffffff);

				if (null != onRecyclerViewListener) onRecyclerViewListener.onItemClick(position);
			}

			@Override public void onItemSelected() {
				LogUtil.e("ViewHolder2 onItemSelected (pos:" + position + ")");
			}

			@Override public void onItemClear() {
				LogUtil.e("ViewHolder2 onItemClear (pos:" + position + ")");
			}
		}

		@Override public int getItemCount() {
			return valueList.size();
		}

		@Override public ViewHolder2 onCreateViewHolder(ViewGroup viewGroup, int i) {
			View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_recycler, viewGroup, false);
			ViewHolder2 viewHolder = new ViewHolder2(view);
			view.setLayoutParams(lpWrapper);
			viewHolder.mImg = (android.widget.ImageView) view.findViewById(R.id.img_anim1);
			viewHolder.mTxt = (TextView) view.findViewById(R.id.tx_test_3);
			return viewHolder;
		}

		@Override public void onBindViewHolder(final ViewHolder2 holder, final int i) {
			final MData md = valueList.get(i);
			holder.position = i;
			holder.mTxt.setText(md.content);
			holder.itemView.setBackgroundColor(md.select ? 0X33000000 : 0Xffffffff);

			// Start a drag whenever the handle view it touched
			holder.itemView.setOnTouchListener(new View.OnTouchListener() {
				@Override public boolean onTouch(View v, MotionEvent event) {
					if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
						if (onDragStartListener != null) onDragStartListener.onStartDrag(holder);
					}
					return false;
				}
			});
		}

		@Override public void onItemMove(int fromPosition, int toPosition) {
			Collections.swap(valueList, fromPosition, toPosition);
			notifyItemMoved(fromPosition, toPosition);
		}

		@Override public void onItemDismiss(int position) {
			valueList.remove(position);
			notifyItemRemoved(position);
		}
	}

}
